package com.vueblog.service.impl;

import com.vueblog.dao.TypeDao;
import com.vueblog.po.Type;
import com.vueblog.service.TypeService;
import com.vueblog.vo.DetailType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeDao typeDao;

    @Override
    public List<Type> findAll() {
        List<Type> types = typeDao.findAll();
        return types;
    }

    @Override
    @Transactional
    public int saveType(Type type) {
        Type t = typeDao.findByName(type.getName());
        if (t==null){
            return typeDao.saveType(type);
        }
        return 0;
    }

    @Override
    @Transactional
    public int deleteType(int id) {
        return typeDao.deleteType(id);
    }

    @Override
    @Transactional
    public int updateType(Type type) {
        Type t = typeDao.findByName(type.getName());
        if (t==null||t.getId()==type.getId()){
            return typeDao.updateType(type);
        }
        return 0;
    }

    @Override
    public List<DetailType> findDetailTypes() {
        return typeDao.findDetailTypes();
    }
}
